<?php

/**
 * Description of DepartController
 * 部门管理
 * @author etpasse
 */
//require_rewrite(MODULE_ROOT."manager/admin/groupManager.class.php");
require_rewrite(MODULE_ROOT . "manager/admin/LogManager.class.php");
require_rewrite(ROOT_PATH . "data/UserConfig.class.php");

class DepartController extends BaseController {

    private $method;

    //private $left_meun;
    public function __construct() {
        parent::__construct();
        //取得页面访问方式
        $this->method = Handler_http::getMethod();
    }

    /**
     * 部门管理列表
     */
    public function departmentList() {
        $list = new ManagingModule();
        $this->tpl->assign('deplist', $list->sql_department_data());
        $this->tpl->display("depart/dep_list.html");
    }

    /**
     * 添加 | 编辑部门
     */
    public function addDep() {
        $dep = new ManagingModule();
        $depid = $_GET['depid'];
        if (!empty($depid)) {
            $data = $dep->sql_department_data($depid);
            $this->tpl->assign('dep', $data);
            $this->tpl->assign('depid', $depid);
        }
        if ($this->method == "POST") {
            $department_name = $_POST['department'];
            $up_department_name = $_POST['up_department'];
            $depid = $_POST['depid'];
            if (!empty($_POST['job']) && $_POST['job'] == 'job') {
                if (UserDao::sqlUpDep($department_name, $depid, $up_department_name)) {
                    LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑部门' . $department_name.'成功');
                    Handler_tool::alertAndRedirect('编辑成功！', HTTPFILEURL . '/depart/departmentList/');
                }else{
                    LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '编辑部门' . $department_name.'失败');
                    Handler_tool::alertAndRedirect('编辑失败！', HTTPFILEURL . '/depart/addDep/?depid=' . $depid);
                }
            } else {
                switch ($dep->auth_dep_add($department_name, $this->user['username'], $up_department_name)) {
                    case 1:
                        LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加部门' . $department_name.'成功');
                        Handler_tool::alertAndRedirect('添加成功！', HTTPFILEURL . '/depart/departmentList/');
                        break;
                    case 3:
                        Handler_tool::alertAndRedirect('该部门名称已存在,请重新输入！', HTTPFILEURL . '/depart/addDep/');
                        break;
                    default :
                        LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '添加部门' . $department_name.'失败');
                        Handler_tool::alertAndRedirect('添加失败！', HTTPFILEURL . '/depart/addDep/');
                        break;
                }
            }
        }
        $this->tpl->assign('dep_date', $dep->sql_department_data());
        $this->tpl->display("depart/add_dep.html");
    }

    /**
     * 删除部门
     */
    public function delEnableDep() {
        header("Content-type:text/html;charset=utf-8");
        $depid = $_GET['depid'];
        $depname = UserDao::getDepartmentList($depid);
        $name = $depname[0]['department_name'];
        if (!empty($depid)) {
            if (UserDao::sqlDelEnableDep($depid)) {
                LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除部门' . $name.'成功');
                Handler_tool::alertAndRedirect('删除成功！', HTTPFILEURL . '/depart/departmentList/');
            }else{
                LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '删除部门' . $name.'失败');
                Handler_tool::alertAndRedirect('删除失败！', HTTPFILEURL . '/depart/departmentList/');
                
            }
        }
    }
}

?>